package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class MineView extends BaseActivity implements View.OnClickListener{
    private String url = "http://192.168.3.77:8088/YiViOnlineServer";// 接口

    private EditText view;
    private Button up;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String token;
    private String userId;
    private String vView;
    private ImageView viewback;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_view);
        initViewView();

        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
    }

    private void initViewView() {
        view = (EditText) findViewById(R.id.view_view);
        up = (Button) findViewById(R.id.view_up);
        viewback = (ImageView) findViewById(R.id.mine_viewback);
        backmain = (ImageView) findViewById(R.id.mineview_backmain);
        backmain.setOnClickListener(this);
        viewback.setOnClickListener(this);
        up.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mineview_backmain:
                startActivity(new Intent(MineView.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.mine_viewback:
                finish();
                break;
            case R.id.view_up:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        UpView();
                    }


                }).start();
                break;
        }
    }
    // 提交建议
    private void UpView() {
        getInView();

        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/user/advice");
        params.addHeader("Content-Type","multipart/form-data");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject json = new JSONObject();
        json.put("content",vView);
        params.setBodyContent(json.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Toast.makeText(MineView.this, "提交成功,感谢你宝贵的建议", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(MineView.this, "请重新输入建议意见", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void getInView() {
        vView = String.valueOf(view.getText());
    }
}
